package com.dogantekin.allseeingeye.map;

import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.dogantekin.allseeingeye.IConstants;
import com.dogantekin.allseeingeye.data.DataFactory;
import com.dogantekin.allseeingeye.eye.Eye;
import com.dogantekin.allseeingeye.sensor.ISensorListener;
import com.dogantekin.allseeingeye.sensor.SensorFactory;
import com.dogantekin.allseeingeye.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class Map extends MapActivity implements IConstants, ISensorListener{
	private static final int MENU_EYE_VIEW   = 1;
	private static final int MENU_EXIT       = 2;
	
	MapView 				mapView;
	MapController			mapController;
	LocationManager 		locationManager;
	MapOverlay				mapOverlay;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
        mapView			= (MapView) findViewById(R.id.mapview);
        mapController	= mapView.getController();
        mapOverlay		= new MapOverlay(getResources());
                
        //set zoom controls
        mapView.setBuiltInZoomControls(true);
        mapController.setCenter(new GeoPoint(new Double(SensorFactory.getInstance(this).getLastKnownLocation().getLatitude()*1E6).intValue(),new Double(SensorFactory.getInstance(this).getLastKnownLocation().getLongitude()*1E6).intValue()));
        mapController.setZoom(13);
        mapView.getOverlays().add(mapOverlay);
		Location lastLocation = SensorFactory.getInstance(this).getLastKnownLocation();        
		mapOverlay.setCurrentLocation(lastLocation);
		mapOverlay.setTargetLocations(DataFactory.getInstance(this).getLocationItems(lastLocation));   
        SensorFactory.getInstance(this).addListener(this);
    }    
    
	@Override
	public void updateLocation(Location location) {
		mapOverlay.setCurrentLocation(location);
		mapOverlay.setTargetLocations(DataFactory.getInstance(this).getLocationItems(location));
		mapView.postInvalidate();
	}

	@Override
	public void updateOrientation(int azimuth, int pitch, int roll) {
		mapOverlay.setAzimuth(azimuth);
		mapOverlay.setPitch(pitch);
		mapOverlay.setRoll(roll);
		mapView.postInvalidate();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuItem mapView = menu.add(0, MENU_EYE_VIEW, 0, getResources().getString(R.string.menu_eye_view));
		mapView.setIcon(android.R.drawable.ic_menu_view);
		MenuItem exit    = menu.add(0, MENU_EXIT, 1, getResources().getString(R.string.menu_exit));
		exit.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case MENU_EYE_VIEW:			
				Intent eyeIntent = new Intent(this,Eye.class);
				startActivity(eyeIntent);
				finish();
				break;
			case MENU_EXIT:			
				finish();
				break;
		}
		return true;
	}	
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}